package org.hekang.controller;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.apache.commons.codec.binary.StringUtils;
import org.hekang.entity.CatDTO;
import org.hekang.entity.PrivateCatDTO;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.ContractException;
import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.sdk.Peer;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.EnumSet;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * 描述: 访问合约控制层
 *
 * @author hekang
 * @date 2024-04-19 16:10
 */
@RestController
@RequestMapping("/cat")
@AllArgsConstructor
public class CatContractController {
    final Contract contractDemo;
    final Network getDefaultNetwork;

    /**
     * 描述: 根据key查询
     *
     * @param key
     * @returns java.util.Map<java.lang.String, java.lang.Object>
     * @author hekang
     * @date 2024/4/19 16:53
     */
    @GetMapping("/{key}")
    public Map<String, Object> queryCatByKey(@PathVariable String key) throws ContractException {
        Map<String, Object> result = Maps.newConcurrentMap();
        // 查询
        byte[] cat = contractDemo.evaluateTransaction("queryCatByKey", key);
        result.put("payload", StringUtils.newStringUtf8(cat));
        result.put("status", "ok");
        return result;
    }

    /**
     * 描述: 新增
     *
     * @param cat
     * @returns java.util.Map<java.lang.String, java.lang.Object>
     * @author hekang
     * @date 2024/4/19 16:58
     */
    @PostMapping("/add")
    public Map<String, Object> createCat(@RequestBody CatDTO cat) throws ContractException, InterruptedException, TimeoutException {
        Map<String, Object> result = Maps.newConcurrentMap();
        byte[] bytes = contractDemo.submitTransaction("createCat", cat.getKey(), cat.getName(), String.valueOf(cat.getAge()), cat.getColor(), cat.getBreed());
        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");
        return result;
    }

    /**
     * 描述: 编辑
     *
     * @param cat
     * @returns java.util.Map<java.lang.String, java.lang.Object>
     * @author hekang
     * @date 2024/4/19 17:13
     */
    @PostMapping("/update")
    public Map<String, Object> updateCat(@RequestBody CatDTO cat) throws ContractException, InterruptedException, TimeoutException {
        Map<String, Object> result = Maps.newConcurrentMap();
        byte[] bytes = contractDemo.submitTransaction("updateCat", cat.getKey(), cat.getName(), String.valueOf(cat.getAge()), cat.getColor(), cat.getBreed());
        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");
        return result;
    }

    /**
     * 描述: 根据key删除
     *
     * @param key
     * @returns java.util.Map<java.lang.String, java.lang.Object>
     * @author hekang
     * @date 2024/4/19 17:15
     */
    @PostMapping("/del/{key}")
    public Map<String, Object> del(@PathVariable String key) throws ContractException, InterruptedException, TimeoutException {
        Map<String, Object> result = Maps.newConcurrentMap();
        // 增加键级别的背书策略  半数同意
        byte[] cat = contractDemo.createTransaction("deleteCat").setEndorsingPeers(getDefaultNetwork.getChannel().getPeers(EnumSet.of(Peer.PeerRole.ENDORSING_PEER))).submit(key);
        result.put("payload", StringUtils.newStringUtf8(cat));
        result.put("status", "ok");
        return result;
    }


    //-------------------------------私有函数控制器------------------------------

    /**
     * 描述: 根据key查询私有数据
     *
     * @param privateCatDTO
     * @returns java.util.Map<java.lang.String, java.lang.Object>
     * @author hekang
     * @date 2024/4/26 10:24
     */
    @GetMapping("/queryPrivateCatByKey")
    public Map<String, Object> queryPrivateCatByKey(@Valid PrivateCatDTO privateCatDTO) throws Exception {

        Map<String, Object> result = Maps.newConcurrentMap();
        byte[] cat = contractDemo.evaluateTransaction("queryPrivateCatByKey", privateCatDTO.getCollection(), privateCatDTO.getKey());

        result.put("payload", StringUtils.newStringUtf8(cat));
        result.put("status", "ok");

        return result;
    }

    /**
     * 描述: 新增私有数据
     *
     * @param privateCat
     * @returns java.util.Map<java.lang.String, java.lang.Object>
     * @author hekang
     * @date 2024/4/26 10:27
     */
    @PostMapping("/createPrivateCat")
    public Map<String, Object> createPrivateCat(@RequestBody PrivateCatDTO privateCat) throws Exception {

        Map<String, Object> result = Maps.newConcurrentMap();
        byte[] bytes = contractDemo.submitTransaction("createPrivateCat", privateCat.getCollection(), privateCat.getKey(), privateCat.getName(), String.valueOf(privateCat.getAge()), privateCat.getColor(), privateCat.getBreed());

        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");
        return result;
    }

    //-------------------------------json查询控制器------------------------------

    /**
     * 描述: 根据名称搜索查询-完整名称
     *
     * @param cat
     * @returns java.util.Map<java.lang.String, java.lang.Object>
     * @author hekang
     * @date 2024/4/29 14:35
     */
    @GetMapping("/getCatByName")
    public Map<String, Object> queryCatByName(@Valid CatDTO cat) throws ContractException {
        Map<String, Object> result = Maps.newConcurrentMap();
        // 查询
        byte[] bytes = contractDemo.evaluateTransaction("queryCatByName", cat.getName());
        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");
        return result;
    }

}
